import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = {
	namespaced: true,
	state: {
		token: localStorage.getItem('parentsToken'),
		userInfo: JSON.parse(localStorage.getItem('userInfo')),
		openId: localStorage.getItem('openId'),
		headimgurl: localStorage.getItem('headimgurl'),
	},
	mutations: {
		setToken(state, token) {
			state.token = token
			localStorage.setItem("parentsToken", token)
			var a = localStorage.getItem(a)
		},
		setLoginInfo(state, loginInfo) {
			state.openId = loginInfo.openId
			localStorage.setItem("openId", loginInfo.openId)
			state.headimgurl = loginInfo.headimgurl
			localStorage.setItem("headimgurl", loginInfo.headimgurl)
		},
		setUserInfo(state, userInfo) {
			console.log(userInfo)
			state.userInfo = userInfo
			localStorage.setItem("userInfo", JSON.stringify(userInfo))
		},
		logout(state) {
			state.token = null;
			state.userInfo = {};
			uni.removeStorage({
				key: 'parentsToken'
			})
			uni.removeStorage({
				key: 'userInfo'
			})
		}
	},
	getter: {
		getToken(state) {
			return state.token
		},
		getUserInfo(state) {
			return state.userInfo
		}
	},
	actions: {
		setToken({
			commit
		}, token) {
			commit('setToken', token);
		},
		setLoginInfo({
			commit
		}, info) {
			commit('setLoginInfo', info);
		},
	}

}

export default store
